package com.zjm.find.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjm.find.pojo.Banner;
import com.zjm.find.pojo.Images;
import com.zjm.find.service.ImagesService;
import com.zjm.find.utils.JSONResult;
import com.zjm.find.utils.TimeUtils;
import com.zjm.find.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.ParseException;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/img")
public class ImageController {

    @Autowired
    private ImagesService imagesService;

    @RequestMapping("/upload")
    public JSONResult uploadImg(@RequestParam(value = "file", required = true) MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        Map<String, String> params = UploadFile.upload(request);
        String bid = request.getParameter("bid");
        String mid = request.getParameter("mid");
        String sid = request.getParameter("sid");
        String url = params.get("url");
        Images images = new Images(bid, mid, sid, url);
        imagesService.createImg(images);
        return JSONResult.successMsg("上传成功");
    }

    @RequestMapping("/createBanner")
    public JSONResult createBanner(Banner banner) throws ParseException {
        Banner banner1 = new Banner(banner.getSuid(), banner.getUrl(), banner.getText(), TimeUtils.getDateTime());
        imagesService.createBanner(banner);
        return JSONResult.successMsg("添加成功");
    }

    @RequestMapping("/selectBanners")
    public JSONResult selectBanners(Banner banner,
                                    @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                    @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo pageInfo = new PageInfo(imagesService.selectBanners(banner));
        return JSONResult.msgAndData("查询成功", pageInfo);
    }

    @PostMapping("/banner")
    public JSONResult banner(Banner banner,
                             @RequestParam(value = "file", required = false) MultipartFile multipartFile,
                             HttpServletRequest request) throws IOException, ParseException {
        Map<String, String> params = UploadFile.upload(request);
        banner.setUrl(params.get("url"));
        if (banner.getType().equals("add")) {
            banner.setCreateTime(TimeUtils.getDateTime());
            if (imagesService.createBanner(banner)) {
                return JSONResult.successMsg("添加成功");
            } else {
                return JSONResult.errorMsg("添加失败");
            }
        } else {
            banner.setUpdateTime(TimeUtils.getDateTime());
            if (imagesService.updateBanner(banner)) {
                return JSONResult.successMsg("更改成功");
            } else {
                return JSONResult.errorMsg("更改");
            }
        }

    }

    @PostMapping("/deleteBanner")
    public JSONResult deleteBanner(Banner banner){
        if (imagesService.deleteBanner(banner)){
            return JSONResult.successMsg("已删除");
        } else {
            return JSONResult.errorMsg("删除失败");
        }
    }

}
